package fr.inria.arles.temperatureManagement.android;

import static fr.inria.arles.temperatureManagement.android.AndroidConstants.*;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import fr.inria.arles.temperatureManagement.ProcessingNodeCommunicator;
import fr.inria.arles.tempmanagement.preferencesstore.service.QueryPort;
import fr.inria.arles.tempmanagement.preferencesstore.service.QueryPort_Service;
import static fr.inria.arles.temperatureManagement.Constants.*;

/**
 * Activity to display user's current reference temperature setting.
 * 
 * @author ajay chhatwal
 */
public class ViewTemp extends Activity {

	private String username, ID, partition;

	private SharedPreferences settings;

	/** Handles event: Button changeTempB clicked */
	private final OnClickListener changeTempBClicked = new OnClickListener() {

		public void onClick(View v) {
			Intent intent = new Intent(ViewTemp.this, ChangeTemp.class);
			intent.putExtra("username", username);
			intent.putExtra("ID", ID);
			intent.putExtra("partition", partition);
			startActivity(intent);
		}
	};

	/** Handles event: Button refreshTempB clicked */
	private final OnClickListener refreshTempBClicked = new OnClickListener() {
		public void onClick(View v) {
			TextView actualTempTV = (TextView) findViewById(R.id.ActualTempTV);
			try {
				actualTempTV.setText(ProcessingNodeCommunicator.getTemp(
						settings.getString("PROCESSING_NODE_IP",
								PROCESSING_NODE_IP), settings.getInt(
								"PROCESSING_NODE_PORT", PROCESSING_NODE_PORT),
						partition)
						+ " degree Celsius");
			} catch (Exception e) {
				Context context = getApplicationContext();
				CharSequence text = "Exception:" + e;
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				Log.e("Exception", e + "");
			}
		}
	};

	/**
	 * Called when the activity is resumed from pause and when the activity is
	 * started
	 * 
	 */
	@Override
	public void onResume() {
		super.onResume();
		TextView tempTV = (TextView) findViewById(R.id.tempTV);
		TextView actualTempTV = (TextView) findViewById(R.id.ActualTempTV);
		try {
			QueryPort_Service service = new QueryPort_Service();
			QueryPort port = service.getQueryPortPort();
			double temp=port.getTempQuery(ID);
			tempTV.setText(temp	+ " degree Celsius");
			
			actualTempTV.setText(ProcessingNodeCommunicator.getTemp(settings
					.getString("PROCESSING_NODE_IP", PROCESSING_NODE_IP),
					settings.getInt("PROCESSING_NODE_PORT",
							PROCESSING_NODE_PORT), partition)
					+ " degree Celsius");

		} catch (Exception e) {
			Context context = getApplicationContext();
			CharSequence text = "Exception:" + e;
			int duration = Toast.LENGTH_LONG;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			Log.e("Exception", e + "");
		}
	}

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            the saved instance state
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_temp);
		Intent i = getIntent();
		username = i.getStringExtra("username");
		ID = i.getStringExtra("ID");
		partition = i.getStringExtra("partition");
		if (username != null) {
			TextView welcomeTV = (TextView) findViewById(R.id.welcomeViewTempTV);
			welcomeTV.setText("Welcome " + username);
		}
		if (partition != null) {
			TextView partitionTV = (TextView) findViewById(R.id.partitionViewTempTV);
			partitionTV.setText(partition);
		}
		settings = getSharedPreferences(PREFS_NAME, 0);
		Button changeTempB = (Button) findViewById(R.id.changeTemp2B);
		changeTempB.setOnClickListener(changeTempBClicked);
		Button refreshActualTempB = (Button) findViewById(R.id.refreshActualTempB);
		refreshActualTempB.setOnClickListener(refreshTempBClicked);

	}

	/*
	 * Called when the activity is first created.
	 * 
	 * @param menu the Menu to be populated
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	/*
	 * Handles item selections
	 * 
	 * @param item the selected MenuItem
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			return true;
		case R.id.exit:
			moveTaskToBack(true);
			return true;
		}
		return false;
	}

}